#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Software: PyCharm
# @Time    : 2019/11/22 10:59
# @Author  : linjinting
# @Site    : 
# @Software: PW4Adatatool
# @File    : TTLPandas.py
# @Function:

import pandas as pd


class PandansBase(object):

    def __init__(self):
        # self._filepath = ""
        self._data_frame = None
        self._data_full = None
        self._data_frame_nan = None

    def read_file(self, filepath, sheetname=0, header=0):
        """
        # 读取文件
        :return: DataFrame
        """
        try:
            self.io = pd.ExcelFile(filepath)
            # pd.read_excel 将Excel文件读入pandas DataFrame
            # sheet_name，指定sheet表，默认为None，即所有sheet
            # header ，指定那一行为列标签，默认为第一行，即header=0
            # 读取全部表格。返回的是一个字典，sheet为可以，sheet的datafrom内容为value
            self._data_frame = pd.read_excel(self.io, sheet_name=sheetname, header=header)
            # print(type(self._data_frame))
            self.io.close()
            return 1
        except BaseException as e:
            print(e)
            return -1

    def read_file_full(self, filepath, header=0):
        """
        # 读取文件
        :return: _data_full (tyep:collections.OrderedDict)
        """
        try:
            io = pd.ExcelFile(filepath)
            # pd.read_excel 将Excel文件读入pandas DataFrame
            # sheet_name，指定sheet表，默认为None，即所有sheet
            # header ，指定那一行为列标签，默认为第一行，即header=0
            # 读取全部表格。返回的是一个字典，sheet为可以，sheet的datafrom内容为value
            self._data_full = pd.read_excel(io, sheet_name=None, header=header, encoding="utf-8")
            # print(type(self._data_full))
            io.close()
            return 1
        except BaseException as e:
            print(e)
            return -1

    # def filter_column(self,colnames):
    #     """
    #     # 筛选列
    #     :param colname: 必须为列表
    #     :return:_data_frame
    #     """
    #     self._data_frame = self._data_frame[colnames]

    def filter_column(self, colnames):
        """
        # 筛选列
        :param colname: 必须为列表
        :return:_data_frame
        """

        self._data_frame = pd.DataFrame(self._data_frame, columns=colnames)

    def isnull(self, value):
        return pd.isnull(value)

    def set_data_frame(self, df):
        self._data_frame = df

    def get_data_frame(self):
        return self._data_frame


class Pandans4AData(PandansBase):
    def __init__(self):
        super(Pandans4AData, self).__init__()

    def filter_data(self, **kwargs):

        for k in kwargs:
            v = kwargs.get(k)
            if isinstance(v, list):
                self._data_frame = self._data_frame[self._data_frame[k].isin(v)]  # 刷选值有多个，使用isin()函数
            else:
                self._data_frame = self._data_frame[self._data_frame[k] == v]
        # print(recode)
        pass

    def left_join(self, r_df, index_key, l_df=None):

        if l_df is None:
            self._data_frame = self._data_frame.join(r_df.set_index(index_key), on=index_key, lsuffix='_l',
                                                     rsuffix='_r')
        else:
            self._data_frame = l_df.join(r_df.set_index(index_key), on=index_key, lsuffix='_l', rsuffix='_r')

    def tables_left_join(self, df_list, index_key, l_df=None):
        _df_index_list = list()
        if isinstance(df_list, list):
            for _df in df_list:
                _df_index_list.append(_df.set_index(index_key))
        else:
            _df_index_list = df_list

        if l_df is None:
            self._data_frame = self._data_frame.set_index(index_key).join(_df_index_list, lsuffix='_l', rsuffix='_r')
        else:
            self._data_frame = l_df.set_index(index_key).join(_df_index_list, lsuffix='_l', rsuffix='_r')

    def dropna_axis_any(self, df=None):
        """
        # axis=0 按行查找，how='any'任意一个值为NAN 则符合条件
        :param df:
        :return:
        """
        if df is None:
            self._data_frame = self._data_frame.dropna(axis=0, how='any')
        else:
            self._data_frame = df.dropna(axis=0, how='any')

    def reset_index(self):
        self._data_frame = self._data_frame.reset_index(drop=True)

    def set_nan_data(self):
        self._data_frame_nan = self._data_frame.isnull().T.any()

    def get_nan_data(self):
        return self._data_frame_nan

    def show_logs(self):
        print("Xxxxxx")
